﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Payton.WP7.Common.Model;
using Newtonsoft.Json.Linq;
using Payton.WP7.Common.Interface;
using System.Globalization;
using System.Diagnostics;

namespace Payton.WP7.Common.Tencent
{
    public class TencentJTokenToUserMapping : MappingBase<JToken, SNSUser>
    {
        public TencentJTokenToUserMapping(IMappingFactory factory)
            : base(factory)
        {

        }

        public override SNSUser Map(JToken token)
        {
            try
            {
                if (token == null || !token.HasValues)
                    return null;

                var user = new SNSUser() { ServiceProvider = _factory.Account.ServiceProvider };

                //TODO: Account
                //user.Account = _factory.Account;
                user.AccountID = _factory.Account.AccountID;
                if (token.SelectToken("uid") != null)
                    user.Id = (long)token.SelectToken("uid");

                user.AccountName = token.SelectToken("name") == null ? null : (string)token.SelectToken("name");
                user.Name = token.SelectToken("nick") == null ? null : (string)token.SelectToken("nick");
                user.Location = token.SelectToken("location") == null ? null : (string)token.SelectToken("location");
                user.Email = token.SelectToken("email") == null ? null : (string)token.SelectToken("email");
                if (token.SelectToken("head") != null && !string.IsNullOrEmpty((string)token.SelectToken("head")))
                    user.ProfileImageUrl = token.SelectToken("head") == null ? null : ((string)token.SelectToken("head") + "/50").Replace("app.qlogo.cn", "t0.qlogo.cn");
                if (token.SelectToken("isvip") != null)
                    user.Verified = (int)token.SelectToken("isvip") == 0 ? false : true;

                if (token.SelectToken("tweet") != null && (string)token.SelectToken("tweet").First.SelectToken("id") != "0")
                    user.LastStatusText = (string)token.SelectToken("tweet").First.SelectToken("text");
                if (token.SelectToken("fansnum") != null)
                    user.FollowersCount = (int)token.SelectToken("fansnum");
                if (token.SelectToken("idolnum") != null)
                    user.FriendsCount = (int)token.SelectToken("idolnum");
                if (token.SelectToken("isidol") != null)
                    user.Following = (bool)token.SelectToken("isidol");
                if (token.SelectToken("tweetnum") != null)
                    user.StatusesCount = (int)token.SelectToken("tweetnum");
                if (token.SelectToken("Ismyidol") != null)
                    user.Following = (int)token.SelectToken("Ismyidol") == 0 ? false : true;
                if (token.SelectToken("Ismyfans") != null)
                    user.Fans = (int)token.SelectToken("Ismyfans") == 0 ? false : true;
                return user;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw ex;
            }

            //throw new NotImplementedException();
        }
    }

    public class TencentJTokenToUserMappingLite : MappingBase<JToken, SNSUser>
    {
        public TencentJTokenToUserMappingLite(IMappingFactory factory)
            : base(factory)
        {
            Key = "Lite";
        }

        public override SNSUser Map(JToken token)
        {
            try
            {
                if (token == null || !token.HasValues)
                    return null;

                var user = new SNSUser();

                user.AccountName = token.SelectToken("name") == null ? null : (string)token.SelectToken("name");
                user.Name = token.SelectToken("nick") == null ? null : (string)token.SelectToken("nick");

                if (token.SelectToken("head") != null && !string.IsNullOrEmpty((string)token.SelectToken("head")))
                    user.ProfileImageUrl = token.SelectToken("head") == null ? null : ((string)token.SelectToken("head") + "/50").Replace("app.qlogo.cn", "t0.qlogo.cn");
                if (token.SelectToken("isvip") != null)
                    user.Verified = (int)token.SelectToken("isvip") == 0 ? false : true;
                return user;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw ex;
            }

            //throw new NotImplementedException();
        }
    }
}
